﻿using BUGCOME.Model.Auth;
using BUGCOME.Model.Common;
using BUGCOME.Model.Log;
using BUGCOME.Model.Logs;
using SqlSugar.IOC;

namespace BUGCOME.ServiceCore.SqlSugar
{
    /// <summary>
    /// 初始化表
    /// </summary>
    public class InitTable
    {
        /// <summary>
        /// 创建db、表
        /// </summary>
        public static void InitDb(bool init)
        {
            var db = DbScoped.SugarScope;
            //可在此处单独更新某个表的结构，无视配置
            //例如：db.CodeFirst.InitTables(typeof(EmailLog));


            if (!init) return;
            StaticConfig.CodeFirst_MySqlCollate = "utf8mb4_general_ci";
            //建库：如果不存在创建数据库存在不会重复创建 
            db.DbMaintenance.CreateDatabase();// 注意 ：Oracle和个别国产库需不支持该方法，需要手动建库 

            #region Auth
            db.CodeFirst.InitTables(typeof(Menu));
            db.CodeFirst.InitTables(typeof(Role));
            db.CodeFirst.InitTables(typeof(RoleMenu));
            db.CodeFirst.InitTables(typeof(SqlDiffLog));
            db.CodeFirst.InitTables(typeof(TeamMember));
            db.CodeFirst.InitTables(typeof(UserBase));
            db.CodeFirst.InitTables(typeof(UserIndividual));
            db.CodeFirst.InitTables(typeof(UserLoginLog));
            db.CodeFirst.InitTables(typeof(UserMsg));
            db.CodeFirst.InitTables(typeof(UserOnlineLog));
            db.CodeFirst.InitTables(typeof(UserRole));
            db.CodeFirst.InitTables(typeof(UserSystem));
            db.CodeFirst.InitTables(typeof(UserTeam));
            #endregion

            #region Common
            db.CodeFirst.InitTables(typeof(DictType));
            db.CodeFirst.InitTables(typeof(DictData));
            db.CodeFirst.InitTables(typeof(SysConfig));
            #endregion

            #region Logs
            db.CodeFirst.InitTables(typeof(OperLog));
            db.CodeFirst.InitTables(typeof(SmsCodeLog));
            #endregion
        }
        /// <summary>
        /// 初始化新表（检查并创建UserOnlineLog表）
        /// </summary>
        public static void InitNewTb()
        {
            var db = DbScoped.SugarScope;
            var t1 = db.DbMaintenance.IsAnyTable(typeof(UserOnlineLog).Name);
            if (!t1)
            {
                db.CodeFirst.InitTables(typeof(UserOnlineLog));
            }
        }
    }
}